package com.achievo.know.domain.infra.hibernate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.achievo.know.domain.File;
import com.achievo.know.domain.FileAttachment;
import com.achievo.know.domain.FileRepository;
import com.achievo.know.domain.TempAttachment;
import com.dayatang.spring.repository.BaseEntityRepositoryHibernateSpring;

@Repository
public class FileRepositoryImpl extends BaseEntityRepositoryHibernateSpring<File,Long> implements FileRepository{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public FileRepositoryImpl() {
		super(File.class);
	}


	@Override
	public void batchDelete(final String[] ids) {
		final String sql="delete from rams_file where id=?";
		final String delUserFavSql="delete from rams_user_favorite where file_id=?";
		final String delAttachSql="delete from rams_file_attachment where FK_ID=? and FK_TYPE=1";
		final String delFaqSql="delete from rams_faq where FK_ID=? and FK_PARENT_ID is null and FAQ_FROM=1";
		final String delFaqSql1="delete from rams_faq where FK_ID=? and FK_PARENT_ID is not null and FAQ_FROM=1";
		BatchPreparedStatementSetter bss=new BatchPreparedStatementSetter(){

			@Override
			public int getBatchSize() {
				return ids.length;
			}

			@Override
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
					ps.setLong(1, Long.parseLong(ids[i]));
			}
			
		};
		jdbcTemplate.batchUpdate(delAttachSql, bss);
		jdbcTemplate.batchUpdate(delUserFavSql, bss);
		jdbcTemplate.batchUpdate(delFaqSql1, bss);
		jdbcTemplate.batchUpdate(delFaqSql, bss);
		jdbcTemplate.batchUpdate(sql, bss);
	}
	
	


	@Override
	public void batchDeleteFavorite(final String[] ids) {
		final String delUserFavSql="delete from rams_user_favorite where file_id=?";
		BatchPreparedStatementSetter bss=new BatchPreparedStatementSetter(){

			@Override
			public int getBatchSize() {
				return ids.length;
			}

			@Override
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
					ps.setLong(1, Long.parseLong(ids[i]));
			}
			
		};
		jdbcTemplate.batchUpdate(delUserFavSql, bss);
		
	}


	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	@Override
	public void addTempAttachment(TempAttachment temp) {
		getHibernateTemplate().save(temp);
	}


	@Override
	public void delTempAttachment(Long attachId) {
		getHibernateTemplate().delete(getHibernateTemplate().get(TempAttachment.class, attachId));
	}


	@Override
	public FileAttachment getAttachment(Long id) {
		return getHibernateTemplate().get(FileAttachment.class, id);
	}


	@Override
	public boolean isAddFavorite(Long id,String username) {
		String queryString="select count(uf.id) from User u,UserFavorite uf where u.id=uf.userId and u.username=? and uf.fileId=? ";
		Object[] params=new Object[]{username,id};
		Long count=(Long)getSingleResult(queryString, params);
		if(count>0)
			return true;
		return false;
	}


	@Override
	public void addDownSum(Long fkId) {
		File f=get(fkId);
		f.addBrowserSum();
		save(f);
	}


	@Override
	public TempAttachment getTempByGuid(String guid) {
		DetachedCriteria c=DetachedCriteria.forClass(TempAttachment.class);
		c.add(Restrictions.eq("guid", guid));
		List<TempAttachment> list = getHibernateTemplate().findByCriteria(c);
		if(list.size()>0)
			return list.get(0);
		return null;
		
	}
	
	
	
}
